package com.shopp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopp.auto.JSONAuthentication;
import com.shopp.entity.CarFood;
import com.shopp.entity.CollectFood;
import com.shopp.mapper.CollectFoodMapper;
import com.shopp.mapper.ShFoodMapper;
import com.shopp.mapper.ShUserMapper;
import com.shopp.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shopp
 * @since 2022-03-04
 */
@RestController
@RequestMapping("/collect-food")
public class CollectFoodController extends JSONAuthentication {
    @Autowired
    CollectFoodMapper collectFoodMapper;
    @Autowired
    ShUserMapper shUserMapper;
    @Autowired
    ShFoodMapper shFoodMapper;
    @ApiOperation(value = "添加收藏商品")
    @PostMapping("/add")
    public void addCollect(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int  userId, @RequestParam("foodId") int foodId) throws Exception {
        CollectFood collectFood=new CollectFood();
        collectFood.setUserId(userId);
        collectFood.setFoodId(foodId);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        wrapper.eq("food_id",foodId);
        if(collectFoodMapper.selectOne(wrapper)!=null){
            Result error = Result.error().message("此商品你已经收藏");
            this.WriteJSON(request, response, error);
        }else{
            if (collectFoodMapper.insert(collectFood)> 0) {
                Result ok = Result.ok().message("添加成功");
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }
    }
    @ApiOperation(value = "删除收藏的商品")
    @PostMapping("/del")
    public void delZiLiao(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = collectFoodMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据用户id查询收藏的商品")
    @PostMapping("/findFoodByUserId")
    public void findFoodByUserId(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int  size, @RequestParam("current") int current,@RequestParam("userId") int userId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<CollectFood> page=new Page<>(current,size);
        Page<CollectFood> pages=collectFoodMapper.selectPage(page,wrapper);
        List<CollectFood> records = pages.getRecords();
        for(CollectFood a:records){
            a.setShUser(shUserMapper.selectById(a.getUserId()));
            a.setShFood(shFoodMapper.selectById(a.getFoodId()));
        }
        int total=shUserMapper.selectCount(wrapper);
        if (records.size() > 0) {
            Result ok = Result.ok().data("foods",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("暂无商品");
            this.WriteJSON(request, response, error);
        }
    }
}

